package com.example.mychat.comparator;

import com.example.mychat.entity.Message;
import com.example.mychat.entity.Redpacket;

import java.util.Comparator;

public class ComparatorTime implements Comparator {
    @Override
    public int compare(Object a, Object b) {
        Message msg1, msg2;
        Redpacket rp1, rp2;
        if (a instanceof Message) {
            msg1 = (Message) a;
            if (b instanceof Message) {
                msg2 = (Message) b;
                return msg1.getTime().compareTo(msg2.getTime());
            } else {
                rp1 = (Redpacket) b;
                return msg1.getTime().compareTo(rp1.getTime());
            }
        } else {
            rp1 = (Redpacket) a;
            if (b instanceof Message) {
                msg1 = (Message) b;
                return rp1.getTime().compareTo(msg1.getTime());
            } else {
                rp2 = (Redpacket) b;
                return rp1.getTime().compareTo(rp2.getTime());
            }
        }
    }
}
